package com.bsj.power.common.config.cached;

import com.alibaba.fastjson2.JSON;
import com.bsj.power.common.config.redis.RedisConstant;
import com.bsj.power.common.def.entity.alarmReport.AlarmReport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import redis.clients.bsj.JedisClusterBSJ;

import java.util.Map;

/**
 * @author LiJiaXin
 * @version 1.0.0
 * @description AlarmCached
 * @time 2024/5/23 9:28
 */
@Component
public class PathAlarmCached {

    @Autowired
    private JedisClusterBSJ jedisClusterBSJ;

    /**
     * 获取围栏报警
     *
     * @param deviceName
     * @param pathId
     * @return com.bsj.power.common.def.entity.alarmReport.AlarmReport
     * @author ljx
     * @time 2024/5/23 9:42
     */
    public AlarmReport hgetPathAlarmInfo(String deviceName, Integer pathId) {
        String alarmStr = jedisClusterBSJ.hget(RedisConstant.PATH_ALARM_REDIS_KEY + deviceName, pathId + "");
        if (alarmStr == null) {
            return null;
        }
        return JSON.parseObject(alarmStr, AlarmReport.class);
    }

    /**
     * 获取围栏报警
     *
     * @param deviceName
     * @return java.util.Map<java.lang.String,java.lang.String>
     * @author ljx
     * @time 2024/6/12 10:54
     */
    public Map<String,String> hgetAllPathAlarmInfo(String deviceName) {
       return jedisClusterBSJ.hgetAll(RedisConstant.PATH_ALARM_REDIS_KEY + deviceName);
    }

    /**
     * 缓存围栏报警信息
     *
     * @param alarmReport
     * @return void
     * @author ljx
     * @time 2024/5/23 9:43
     */
    public void hsetPathAlarmInfo(AlarmReport alarmReport) {
        if (alarmReport != null) {
            jedisClusterBSJ.hset(RedisConstant.PATH_ALARM_REDIS_KEY + alarmReport.getDeviceName(), alarmReport.getPathId() + "", JSON.toJSONString(alarmReport));
        }
    }

    /**
     * 删除围栏报警信息
     *
     * @param deviceName
     * @param pathId
     * @return void
     * @author ljx
     * @time 2024/5/23 9:46
     */
    public void hdelPathAlarmInfo(String deviceName, Integer pathId) {
        jedisClusterBSJ.hdel(RedisConstant.PATH_ALARM_REDIS_KEY + deviceName, pathId + "");
    }

    /**
     * 删除围栏报警信息
     *
     * @param deviceName
     * @return void
     * @author ljx
     * @time 2024/5/23 9:53
     */
    public void hdelPathAlarmInfo(String deviceName) {
        jedisClusterBSJ.del(RedisConstant.PATH_ALARM_REDIS_KEY + deviceName);
    }

//    /**
//     * 获取所有围栏信息
//     *
//     * @return java.util.List<com.bsj.power.common.def.entity.path.Path>
//     * @author ljx
//     * @time 2024/5/21 17:25
//     */
//    public List<Path> hgetAllPathInfo() {
//        Map<String, String> pathMap = jedisClusterBSJ.hgetAll(RedisConstant.PATH_REDIS_KEY);
//        if (CollectionUtils.isEmpty(pathMap)) {
//            return null;
//        }
//        return pathMap.entrySet().stream().map(value -> {
//            return JSON.parseObject(value.getValue(), Path.class);
//        }).collect(Collectors.toList());
//    }

}
